package com.ruoyi.bizsys.controller;

import com.ruoyi.bizsys.domain.SmsAreaNumberConfig;
import com.ruoyi.bizsys.service.ISmsAreaNumberConfigService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短信路由配置Controller
 *
 * @author african-loan
 * @date 2021-09-30
 */
@RestController
@RequestMapping("/bizsys/sms_route_config")
public class SmsAreaNumberConfigController extends BaseController {
    @Autowired
    private ISmsAreaNumberConfigService smsAreaNumberConfigService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询短信路由配置列表
     */
    @PreAuthorize("@ss.hasPermi('bizsys:sms_route_config:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmsAreaNumberConfig smsAreaNumberConfig) {
        startPage();
        List<SmsAreaNumberConfig> list = smsAreaNumberConfigService.selectSmsAreaNumberConfigList(smsAreaNumberConfig);
        return getDataTable(list);
    }

    /**
     * 导出短信路由配置列表
     */
    @PreAuthorize("@ss.hasPermi('bizsys:sms_route_config:export')")
    @Log(title = "短信路由配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SmsAreaNumberConfig smsAreaNumberConfig) {
        List<SmsAreaNumberConfig> list = smsAreaNumberConfigService.selectSmsAreaNumberConfigList(smsAreaNumberConfig);
        ExcelUtil<SmsAreaNumberConfig> util = new ExcelUtil<SmsAreaNumberConfig>(SmsAreaNumberConfig.class);
        return util.exportExcel(list, "sms_route_config");
    }

    /**
     * 获取短信路由配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('bizsys:sms_route_config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(smsAreaNumberConfigService.selectSmsAreaNumberConfigById(id));
    }

    /**
     * 新增短信路由配置
     */
    @PreAuthorize("@ss.hasPermi('bizsys:sms_route_config:add')")
    @Log(title = "短信路由配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmsAreaNumberConfig smsAreaNumberConfig) {
        Long userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
        smsAreaNumberConfig.setCreateBy(String.valueOf(userId));
        return toAjax(smsAreaNumberConfigService.insertSmsAreaNumberConfig(smsAreaNumberConfig));
    }

    /**
     * 修改短信路由配置
     */
    @PreAuthorize("@ss.hasPermi('bizsys:sms_route_config:edit')")
    @Log(title = "短信路由配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmsAreaNumberConfig smsAreaNumberConfig) {
        Long userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
        smsAreaNumberConfig.setUpdateBy(String.valueOf(userId));
        return toAjax(smsAreaNumberConfigService.updateSmsAreaNumberConfig(smsAreaNumberConfig));
    }

    /**
     * 删除短信路由配置
     */
    @PreAuthorize("@ss.hasPermi('bizsys:sms_route_config:remove')")
    @Log(title = "短信路由配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(smsAreaNumberConfigService.deleteSmsAreaNumberConfigByIds(ids));
    }
}
